package org.example.app.order.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.Assert;
import org.apache.commons.lang3.StringUtils;
import org.example.app.order.assembler.OrderAssembler;
import org.example.client.order.dto.clientobject.OrderCO;
import org.example.domain.order.entity.OrderEntity;
import org.example.domain.order.gateway.OrderGateway;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class OrderGetByOrderNoQueryExe {

    @Resource
    private OrderGateway orderGateway;
    @Resource
    private OrderAssembler assembler;

    public SingleResponse<OrderCO> execute(String orderNo) {
        Assert.isTrue(StringUtils.isNotBlank(orderNo), "订单号不能为空");
        OrderEntity entity = orderGateway.getByOrderNo(orderNo);
        Assert.notNull(entity, "订单不存在");
        // 延迟加载订单列表
        entity.setOrderItemList(entity.getOrderItemList());
        return SingleResponse.of(assembler.toCO(entity));
    }
}
